package org.nokatag.system;

import java.io.InputStream;
import java.net.URL;
import java.sql.ResultSetMetaData;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nokatag.jdom.Document;
import org.nokatag.jdom.Element;
import org.nokatag.jdom.input.SAXBuilder;
/**
 * 常用方法类
 * @author rebin
 */
public class Function {
	private static final String IE="IE";
	private static final String FF="FF";
	private static final String OP="OP";
	private static final String SA="SA";
	private static final String CH="CH";
	private static final String NA="NA";
	
	/**
	 * 将字符串转换成对像数组,[vvv],[aaa]
	 * @param sv
	 * @return
	 */
	public static List<String> StringToList(String sv){
		List<String> vl = new ArrayList<String>();
		if(sv==null || sv.trim().length()<1)
			return vl;
		String[] listv=sv.split("\\]");
		try{
			for(String s:listv){
				String[] sl=s.split("\\[");
				if(sl.length>1){
					String sa=sl[1];
					vl.add(sa);
				}
			}
			return vl;
		}catch(Exception se){
			se.printStackTrace();
			return null;
		}
	}
	/**
	 * 将字符串转换成对像数组,[id=a,name=ew,value=fds],[id=b,name=ew,value=fds],[id=c,name=ew,value=fds]
	 * @param sv
	 * @return
	 */
	public static List<Map<String,String>> StringToObjs(String sv){
		List<Map<String,String>> vl = new ArrayList<Map<String,String>>();
		if(sv==null || sv.trim().length()<1)
			return vl;
		String[] listv=sv.split("\\]");
		try{
			for(String s:listv){
				String[] sl=s.split("\\[");
				if(sl.length>1){
					String sa=sl[1];
					Map<String,String> vs=new HashMap<String,String>();
					for(String v:sa.split(",")){
						vs.put(v.split("=")[0], v.split("=")[1]);
					}
				vl.add(vs);
				}
			}
			return vl;
		}catch(Exception se){
			se.printStackTrace();
			return null;
		}
	}
	/**
	 * 将字符串转换成对像数组,cname=所属银行,3=data_row_1
	 * @param sv
	 * @return
	 */
	public static Map<String,Map<String,String>> StringToMaps(String sv){
		Map<String,Map<String,String>> vl = new HashMap<String,Map<String,String>>();
		if(sv==null || sv.trim().length()<1)
			return vl;
		String[] listv=sv.split("\\]");
		try{
			for(String s:listv){
				String[] sl=s.split("\\[");
				if(sl.length>1){
					String sa=sl[1];
					Map<String,String> vs=new HashMap<String,String>();
					for(String v:sa.split(",")){
						vs.put((v.split("=")[0]).trim(), (v.split("=")[1]).trim());
					}
					vl.put((sa.split(",")[0].split("=")[1]).trim(), vs);
				}
			}
			return vl;
		}catch(Exception se){
			se.printStackTrace();
			return null;
		}
	}
	/**
	 * 将字符数组转换成长整型数组
	 * @param stringarry
	 * @return
	 */
	public static Long[] stringarryTolongArry(String[] stringarry){
		Long[] longdata = new Long[stringarry.length];
		for(int i=0;i<stringarry.length;i++){
			longdata[i]=Long.parseLong(stringarry[i]);
		}
		return longdata;
	}
	/**
	 * 字符串转换成长整型数组
	 * @param stringar
	 * @param splis
	 * @return
	 */
	public static Long[] stringTolongArry(String stringar,String splis){
		String[] stringarry = stringar.split(splis);
		Long[] longdata = new Long[stringarry.length];
		for(int i=0;i<stringarry.length;i++){
			longdata[i]=Long.parseLong(stringarry[i]);
		}
		return longdata;
	}
	/**
	 * 格式化取当前时间
	 * @param fomrat
	 * @return
	 */
	public static String NewDate(String fomrat){
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(fomrat);
		java.util.Date date = new java.util.Date();
		String newdate = bartDateFormat.format(date);
		return newdate;
	}
	/**
	 * 数组转换成字符串
	 * @param strs
	 * @return
	 */
	public static String ArayToString(ArrayList<String> strs){
		String strns = "";
		if(strs!=null && !strs.isEmpty()){
			for(String str : strs){
				strns+=","+str;
			}
			if(strns.indexOf(",")!=-1)
				strns=strns.substring(1,strns.length());
		}
		return strns;
	}
	/**
	 * 数组转换成字符串
	 * @param strs
	 * @return
	 */
	public static String ArayToString(String[] strs){
		String strns = "";
		if(strs!=null && strs.length>0){
			for(String str : strs){
				strns+=","+str;
			}
			if(strns.indexOf(",")!=-1)
				strns=strns.substring(1,strns.length());
		}
		return strns;
	}
	
	/**
	 * 数组转换成字符串带单引号
	 * @param strs
	 * @return
	 */
	public static String ArayToStringY(ArrayList<String> strs){
		String strns = "";
		if(strs!=null && !strs.isEmpty()){
			for(String str : strs){
				strns+="','"+str;
			}
			if(strns.indexOf("',")!=-1)
				strns=strns.substring(2,strns.length())+"'";
		}
		return strns;
	}
	/**
	 * 格式化html时间
	 * @param datestr
	 * @return
	 * @throws ParseException 
	 */
	public static String formatHtmlDate(String datestr,String fomrat){
		try{
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(fomrat);
			Date date = bartDateFormat.parse(datestr);
			String newdate = bartDateFormat.format(date);
			return newdate;
		}catch(Exception se){
			BugInfoWrint.Print(se);
			return "格式化错误 ";
		}
	}
	
	/**
	 * 字符串转换成数组字符串
	 * @param str
	 * @return
	 */
	public static String  StringToArray(String str){
		if(str==null)
			return "";
		String[] strn = str.split(",");
		String ssn="{";
		for(String st:strn){
			ssn+="'"+st.trim()+"',";
		}
		ssn = ssn.substring(0,ssn.lastIndexOf(","));
		ssn+="}";
		return ssn;
	}
	
	/**
	 * 取request变量
	 * @param queryString
	 * @param parname
	 * @return
	 */
	public static String requestQueryString(String queryString,String parname){
		if(parname==null || parname.trim().length()<1)
			return null;
		if(queryString==null || queryString.trim().length()<1)
			return null;
		String[] sp=queryString.split("&");
		String rvalue="";
		for(String p:sp){
			if(p.indexOf("=")!=-1){
				try{
					String name=p.split("=")[0];
					String value=p.split("=")[1];
					if(name.equals(parname))
						rvalue=value;
				}catch(Exception se){}
			}
			
		}
		return rvalue;
	}
	/**
	 * 取语文key
	 * @param request
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public  static String lung(HttpServletRequest request,String key){
		try{
			if(null!=request.getAttribute(key)){
				return request.getAttribute(key).toString();
			}
			if(request.getHeader("accept-language")==null) return null;
			String[] mark = request.getHeader("accept-language").split(",");//客户端语言
			InputStream input = null;
			for(String luang:mark){
				luang = luang.split(";")[0].toLowerCase();
				String sfp = Concest.LANGUAGEPATH+"language_"+luang+".xml";
				input = findInputStream(sfp);
				if(null!=input){
					break;
				}
			}
			if(input==null){//默认加载语言
				String fp=Concest.LANGUAGEPATH+"language_"+Concest.DEFAULTLANGUAGE+".xml";
				input = findInputStream(fp);
			}
			SAXBuilder saxbuilder = new SAXBuilder(); // 新建立构造器 
			Document doc = saxbuilder.build(input);//加载语言文件
			Element rootelementroot=doc.getRootElement(); 
			List<Element>  datalist = rootelementroot.getChildren("option");
			for(Element en:datalist){
				if(en.getAttributeValue("key").toString().equals(key))
					return en.getText();
			}
		}catch(Exception se){
			BugInfoWrint.Print(se);
		}
		return null;
	}
	/**
	 * 判断方法
	 * @param str [key=2a,show=是],[key=34,show=滞]
	 * @return
	 */
	public static String ifs(String str,String value){
		List<Map<String,String>> Lstr= Function.StringToObjs(str);
		for(Map<String,String> m:Lstr){
			if(m.get("key").equalsIgnoreCase(value)){
				return m.get("show");
			}
		}
		return "";
	}
	
	protected static InputStream findInputStream(String resourcePath){
		 try{
			if(resourcePath.startsWith("/"))
				resourcePath=resourcePath.substring(1);
			URL sb =Thread.currentThread().getContextClassLoader().getResource(resourcePath);
			return sb.openStream();
		 }catch(Exception se){
			// System.out.println(resourcePath);
			//BugInfoWrint.Print(se);
		 }
	     return null;
	 }
	
	public static boolean isStringNotNull(String str){
		if(str!=null && str.trim().length()>0)
			return true;
		else
			return false;
	}
	/**
	 * 返回浏览器的类型
	 * @param request
	 * @return
	 */
	public static String BrowserType(HttpServletRequest request){
		if(request.getHeader("USER-AGENT").toLowerCase().indexOf("msie")!=-1){
			return Function.IE;
		}else if(request.getHeader("USER-AGENT").toLowerCase().indexOf("firefox")!=-1){
			return Function.FF;
		}else if(request.getHeader("USER-AGENT").toLowerCase().indexOf("opera")!=-1){
			return Function.OP;
		}else if(request.getHeader("USER-AGENT").toLowerCase().indexOf("safari")!=-1){
			return Function.SA;
		}else if(request.getHeader("USER-AGENT").toLowerCase().indexOf("chrome")!=-1){
			return Function.CH;
		}else if(request.getHeader("USER-AGENT").toLowerCase().indexOf("navigator")!=-1){
			return Function.NA;
		}
		
		return null;
	}
	/**
	 * 检查某列是否存在
	 * @param rsmd
	 * @param fn
	 * @return
	 */
    public static boolean TableFieldIsExit(ResultSetMetaData rsmd,String fn){
    	try{
    		if(rsmd==null || fn==null)
    			return false;
    		int columnCount = rsmd.getColumnCount();
	    	for(int i=1;i<=columnCount;i++){
	    	   if(rsmd.getColumnName(i)!=null && rsmd.getColumnName(i).equalsIgnoreCase(fn))
	    			   return true;
	    	}
    	}catch(Exception se){
    		BugInfoWrint.Print(se);
    	}
    	return false;
    }
	//===============
	public static void main(String[] args){
		String str="[key=2a,show=是],[key=34,show=滞]";
		System.out.println(Function.ifs(str,"2a"));
	}
}

